import { createRouter, createWebHistory } from 'vue-router';

const routes = [
  {
    path: '/' ,
    name: 'root' ,
    redirect: '/sign/up'
  },
  {
    path: '/sign/up' ,
    name: 'signUp' ,
    components: {
      header: () => import( '@/views/Header.vue' ) ,
      main: () => import( '@/views/SignUp.vue' ) ,
    }
  },
  {
    path: '/sign/in' ,
    name: 'signIn' ,
    // 路由独享的守卫
    beforeEnter(){
      // 从 sessionStorage 中获取 user 项目
      let username = sessionStorage.getItem( 'username' );
      // 如果 username 是存在的，则表示已经登录过
      if( username ) {
        console.log( '你已经登录了' );
        return { name: 'main' }
      }
    },
    components: {
      header: () => import( '@/views/Header.vue' ) ,
      main: () => import( '@/views/SignIn.vue' ) ,
    }
  },
  {
    path: '/main' ,
    name: 'main' ,
    components: {
      header: () => import( '@/views/Header.vue' ) ,
      main: () => import( '@/views/Main.vue' ) ,
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

export default router
